<template>
  <div class="course">
    <div class="head">
      <span>我上传的课程</span>
      <button class="blue-btn-outline" @click="dialogFormVisible = true">+上传新课程</button>
    </div>
    <div class="xyd-el-table2">
      <el-table
        ref="multipleTable"
        :data="course"
        :border="false"
        tooltip-effect="dark"
        style="width: 100%"
        v-loading.body="loading">
        <el-table-column
          prop="create_time"
          label="上传时间"
          width="200">
        </el-table-column>
        <el-table-column
          prop="title"
          label="课程名称"
          width="200">
        </el-table-column>
        <el-table-column
          prop="price"
          label="语点"
          width="120">
        </el-table-column>
        <el-table-column
          prop="option"
          label="操作"
          show-overflow-tooltip>
          <template scope="scope">
            <a href="javascript:;" @click="handleDelete(scope.row)">删除</a>
            <a href="javascript:;" @click="handleDetail(scope.row)">编辑</a>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        :page-size="offset"
        layout="prev, pager, next"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog title="上传课程" top="20px" :visible.sync="dialogFormVisible">
      <el-form :model="form" style="padding: 60px 140px 0 140px;">
        <el-form-item label="课程名称：" :label-width="formLabelWidth">
          <el-input v-model="form.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="语点类型：" :label-width="formLabelWidth">
          <el-select
            v-model="form.yd_type"
            placeholder="语点类型"
            style="width:100%;"
          >
            <el-option
              v-for="item,i in types"
              :key="i"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所需语点：" :label-width="formLabelWidth">
          <el-input v-model="form.price" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="课程简介：" :label-width="formLabelWidth">
          <el-input v-model="form.intro" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="" :label-width="formLabelWidth">
          <el-button @click="upload">选择文件</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleConfirm" :loading="loading" style="width: 300px;">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getInfo, getCourse } from 'api/user'
import pick from 'utils/file/picker'
export default {

  name: 'index',

  data() {
    return {
      course: [],
      dialogFormVisible: false,
      formLabelWidth: '120px',
      types: [
        { label: '普通', value: 0 },
        { label: '钻石', value: 1 }
      ],
      form: {
        title: '',
        yd_type: 0,
        price: '',
        intro: ''
      },
      offset: 10,
      page: 1,
      total: 0,
      loading: false
    };
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.form = {
        title: '',
        yd_type: 0,
        price: '',
        intro: ''
      }
      this.loading = true
      getCourse()
      .then(res => {
        console.log(res)
        this.loading = false
        this.course = res.course
        this.total = res.page.last_page * this.offset
      })
      .catch(err => {
        console.log(err)
        this.loading = false
      })
    },
    handleDelete(row) {
      console.log(row)
      getCourse({
        id: row.id
      }, 'delete')
      .then(res => {
        this.message({
          message: '删除成功',
          type: 'error'
        })
        this.fetchData()
      })
      .catch(err => {
        console.log(err)
        this.$message({
          message: '删除失败',
          type: 'error'
        })
      })
    },
    handleDetail(row) {
      getCourse({
        id: row.id
      })
      .then(res => {
        console.log(res)
        this.dialogFormVisible = true
        this.form = res.course
      })
      .catch(err => console.log(err))
    },
    handleConfirm() {
      this.loading = true
      getCourse({
        ...this.form,
        course: this.file
      }, 'post')
      .then(res => {
        console.log(res)
        this.loading = false
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        this.fetchData()
      })
      .catch(err => {
        console.log(err)
        this.$message({
          message: err.msg,
          type: 'error'
        })
        this.loading = false
      })
    },
    upload() {
      pick().then(file => {
        console.log(file)
        this.file = file
      })
      .catch(err => console.log(err))
    }
  }
};
</script>

<style lang="scss" scoped>
.course{
  .head{
    padding: 10px 0;
    >span{
      float: left;
    }
    >button{
      float: right;
      width: 84px;
    }
  }
}
</style>
